package crawler.douban;

import com.alibaba.fastjson.JSON;
import mongo.DateUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.jsoup.nodes.Document;

import java.util.Date;

/**
 * USER: lintc 【lintiancong@zhuojianchina.com】
 * DATE: 2017-09-20 10:12
 */
public class Movie {
    /**
     * 介绍页地址
     */
    String url;
    /**
     * 豆瓣ID
     */
    String id;
    /**
     * 电影名称
     */
    String name;
    /**
     * 评论数
     */
    int commentCount;
    /**
     * 星级评分
     */
    float startLevel;
    /**
     * 评分人次
     */
    int voteCount;
    /**
     * 导演
     */
    String director;
    /**
     * 演员
     */
    String[] actors;
    // 上映日期
    String releaseDate;
    // 电影类型
    String[] movieType;
    // 地区
    String[] area;
    // 入库（数据库）时间
    String insertTime;

    public Movie() {}

    public Movie(JSONObject obj) {
        this.insertTime = DateUtil.dateToString(new Date(), "yyyy-MM-dd HH:mm:ss");
        this.url = obj.optString("url");
        this.id = obj.optString("id");
        this.name = obj.optString("title");
        this.startLevel = Float.parseFloat(obj.optString("score"));
        this.voteCount = obj.optInt("vote_count");
        this.actors = jsonToAry(obj.optJSONArray("actors"));
        this.movieType = jsonToAry(obj.optJSONArray("types"));
        this.releaseDate = obj.optString("release_date");
        this.area = jsonToAry(obj.optJSONArray("regions"));
    }

    public Movie(String movieId, Document doc) {
        this.insertTime = DateUtil.dateToString(new Date(), "yyyy-MM-dd HH:mm:ss");
        this.url = movieId;
        this.id = getMovieId(movieId);
        this.name = doc.select("title").text();
        this.startLevel = Float.parseFloat(StringUtils.isBlank(doc.select(".rating_num").text()) ? "0" : doc.select(".rating_num").text());
        this.actors = doc.select(".subject.clearfix .actor").select("a[rel*=starring]").text().split(" ");
        this.movieType = doc.select(".subject.clearfix").select("[property*=genre]").text().split(" ");
        this.releaseDate = getDate(doc);
        this.area = doc.select(".j.a_show_login.lnk-sharing").attr("data-desc").split("/")[2].split(" ");
        setHtml(doc);
    }

    public void setHtml(Document doc) {
        if (doc.select(".mod-hd .pl a").size() > 0) {
            this.commentCount = Integer.parseInt(doc.select(".mod-hd .pl a").get(0).text().split(" ")[1]);
        }
        this.director = doc.select(".subject").select(".clearfix").select("a[rel*=directedBy]").text();
    }

    public static String[] jsonToAry(JSONArray array) {
        String[] strings = new String[array.size()];
        for (int i = 0; i < array.size(); i++) {
            strings[i] = array.get(i).toString();
        }
        return strings;
    }

    /**
     * 获取上映日期
     * @param doc
     * @return
     */
    public String getDate(Document doc) {
        String timeStr = doc.select(".subject.clearfix").select("[property*=initialReleaseDate]").get(0).text();
        if (StringUtils.isNotBlank(timeStr)) {
            if (timeStr.length() >= 10) {
                timeStr = timeStr.substring(0, 10);
            } else {
                timeStr = timeStr.substring(0, 4);
            }
        }
        return timeStr;
    }

    /**
     * 获取电影ID
     * @param url
     * @return
     */
    public String getMovieId(String url) {
        return url.split("/")[4];
    }

    public int getVoteCount() {
        return voteCount;
    }

    public void setVoteCount(int voteCount) {
        this.voteCount = voteCount;
    }

    public String getInsertTime() {
        return insertTime;
    }

    public void setInsertTime(String insertTime) {
        this.insertTime = insertTime;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getCommentCount() {
        return commentCount;
    }

    public void setCommentCount(int commentCount) {
        this.commentCount = commentCount;
    }

    public float getStartLevel() {
        return startLevel;
    }

    public void setStartLevel(float startLevel) {
        this.startLevel = startLevel;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getDirector() {
        return director;
    }

    public void setDirector(String director) {
        this.director = director;
    }

    public String getReleaseDate() {
        return releaseDate;
    }

    public void setReleaseDate(String releaseDate) {
        this.releaseDate = releaseDate;
    }

    public String[] getActors() {
        return actors;
    }

    public void setActors(String[] actors) {
        this.actors = actors;
    }

    public String[] getMovieType() {
        return movieType;
    }

    public void setMovieType(String[] movieType) {
        this.movieType = movieType;
    }

    public String[] getArea() {
        return area;
    }

    public void setArea(String[] area) {
        this.area = area;
    }

    public String toString() {
        return JSON.toJSONString(this);
    }
}

